package com.example.openguass.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.openguass.entity.Dish;
import com.example.openguass.entity.DishFlavor;
import com.example.openguass.entity.OrderDetail;
import com.example.openguass.mapper.OrderDetailMapper;
import com.example.openguass.service.DishFlavorService;
import com.example.openguass.service.DishService;
import com.example.openguass.service.OrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.openguass.vo.DishVO;
import com.example.openguass.vo.OrderDetailPageVO;
import com.example.openguass.vo.OrderDetailVO;
import com.example.openguass.vo.TopDish;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {
    private OrderDetailMapper orderDetailMapper;
    private DishFlavorService dishFlavorService;
    private DishService dishService;
    @Autowired
    private void setOrderDetailMapper(OrderDetailMapper orderDetailMapper){
        this.orderDetailMapper = orderDetailMapper;
    }
    @Autowired
    private void setDishFlavorService(DishFlavorService dishFlavorService){
        this.dishFlavorService = dishFlavorService;
    }
    @Autowired
    private void setDishService(DishService dishService){
        this.dishService = dishService;
    }

    @Override
    public Boolean insert(OrderDetailVO orderDetailVO) {
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderDetailVO,orderDetail);
        orderDetail.setCreateDate(LocalDateTime.now());
        orderDetail.setPrice(orderDetail.getAmount() * orderDetail.getNumber());
        //获得口味进行插入
        DishFlavor dishFlavorByDIshId = dishFlavorService.getDishFlavorByDIshId(orderDetailVO.getDishid());
        if (dishFlavorByDIshId != null){
            orderDetail.setDishflavor(dishFlavorByDIshId.getName());
        }
        return orderDetailMapper.insert(orderDetail) > 0;
    }

    @Override
    public Boolean update(OrderDetailVO orderDetailVO) {
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderDetailVO,orderDetail);
        orderDetail.setUpdateDate(LocalDateTime.now());
        return orderDetailMapper.updateById(orderDetail) > 0;
    }

    @Override
    public IPage<OrderDetail> findPage(OrderDetailPageVO pageVO) {
        Page<OrderDetail> page = new Page<>(pageVO.getPageNo(),pageVO.getPageSize());
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        return orderDetailMapper.selectPage(page,wrapper);
    }

    @Override
    public List<OrderDetail> getByOrdersId(String orderId) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid",orderId);
        return orderDetailMapper.selectList(wrapper);
    }

    @Override
    public Boolean removeByOrderId(String orderId) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid",orderId);
        return orderDetailMapper.delete(wrapper) > 0;
    }

    @Override
    public List<TopDish> getTopDish() {
        List<TopDish> topDishes = orderDetailMapper.getTopDishs();
        int i = 0;
        for (TopDish topDish : topDishes){
            Dish dish = dishService.getById(topDish.getName());
            if (dish != null){
                topDish.setName(dish.getName());
            }
//            topDishes.remove(i);
            i++;
        }
        return topDishes.size()>10?topDishes.subList(0,10):topDishes;
    }
}
